﻿namespace Sample.JQuery {
	using System;
	using System.Web.UI;
	using System.Text;
	using NJQuery;

	public partial class AccordionPage : Page {

		protected override void OnLoad(System.EventArgs e) {
			base.OnLoad(e);

			AnimationDropdown.SelectedIndexChanged += new EventHandler(AnimationDropdown_SelectedIndexChanged);
			ChangeStartAccordion.ChangeStart += new EventHandler<AccordionChangeStartEventArgs>(ChangeStartAccordion_ChangeStart);
			SelectPanel1Button.Click += new EventHandler(SelectPanel1Button_Click);
			SelectPanel3Button.Click += new EventHandler(SelectPanel3Button_Click);
			PanelActivatingPanel1.Activating += new EventHandler<AccordionPanelActivatingEventArgs>(PanelActivatingPanel1_Activating);
			PanelActivatingPanel2.Activating += new EventHandler<AccordionPanelActivatingEventArgs>(PanelActivatingPanel2_Activating);
			PanelActivatingPanel3.Activating += new EventHandler<AccordionPanelActivatingEventArgs>(PanelActivatingPanel3_Activating);

		}

		private void AnimationDropdown_SelectedIndexChanged(object sender, EventArgs e) {
			AccordionAnimation animated = AccordionAnimation.Parse(AnimationDropdown.SelectedValue);
			ChangeStartAccordion.Animated = animated;
			PanelActivatingAccordion.Animated = animated;
		}

		private void ChangeStartAccordion_ChangeStart(object sender, AccordionChangeStartEventArgs e) {

			StringBuilder info = new StringBuilder();

			if(e.NewActive == null)
				info.Append("The accordion was collapsed client-side");
			else {
				info.Append("Panel ");
				info.Append((ChangeStartAccordion.Panels.IndexOf(e.NewActive) + 1).ToString());
				info.Append(" was activated client-side");
			}

			if(CancelChangeStartCheckBox.Checked){
				e.Cancel = true;
				info.Append(", but canceled at");
			}

			info.Append(" ");
			info.Append(DateTime.Now.ToLongTimeString());
			info.Append(".");

			ChangeStartInfoLabel.Text = Server.HtmlEncode(info.ToString());

		}

		private void SelectPanel1Button_Click(object sender, EventArgs e) {
			ChangeStartInfoLabel.Text = "Panel 1 was activated server-side at " 
				+ Server.HtmlEncode(DateTime.Now.ToLongTimeString());
			ChangeStartAccordion.Activate(0);
		}

		private void SelectPanel3Button_Click(object sender, EventArgs e) {
			ChangeStartInfoLabel.Text = "Panel 3 was activated server-side at "
				+ Server.HtmlEncode(DateTime.Now.ToLongTimeString());
			ChangeStartAccordion.Activate(ChangeStartPanel3);
		}

		private void PanelActivatingPanel1_Activating(object sender, AccordionPanelActivatingEventArgs e) {
			PanelActivatingTimestampLabel1.Text = Server.HtmlEncode(DateTime.Now.ToLongTimeString());
			PanelActivatingPanel1.PostBackOnChangeStart = false;
		}
		private void PanelActivatingPanel2_Activating(object sender, AccordionPanelActivatingEventArgs e) {
			PanelActivatingTimestampLabel2.Text = Server.HtmlEncode(DateTime.Now.ToLongTimeString());
			PanelActivatingPanel2.PostBackOnChangeStart = false;
		}
		private void PanelActivatingPanel3_Activating(object sender, AccordionPanelActivatingEventArgs e) {
			PanelActivatingTimestampLabel3.Text = Server.HtmlEncode(DateTime.Now.ToLongTimeString());
			PanelActivatingPanel3.PostBackOnChangeStart = false;
		}

	}

}